# Copyright (C) 2015 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if ![supports_reverse] {
    return
}

# Test aarch64 instruction recording.

if {![is_aarch64_target]} then {
    verbose "Skipping aarch64 instruction recording tests."
    return
}

standard_testfile

if {[prepare_for_testing $testfile.exp $testfile $srcfile \
	 [list debug]]} {
    untested ${testfile}.exp
    return -1
}
if { ![runto main] } then {
    fail "run to main"
    return
}

# In each function FUNC, GDB turns on process record, and single step
# until program goes to the end of the function.  Then, single step
# backward.  In each of forward single step and backward single step,
# the contents of registers are saved, and test compares them.  If
# there is any differences, a FAIL is emitted.

proc test { func } {
    global hex decimal
    global gdb_prompt

    with_test_prefix "$func" {
	gdb_breakpoint $func
	gdb_test "continue"

	set last_insn ""
	set test "disassemble $func"
	gdb_test_multiple $test $test {
	    -re ".*($hex) <\\+$decimal>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
		set last_insn $expect_out(1,string)
	    }
	}
	if { $last_insn == "" } {
	    fail "find the last instruction of function $func"
	}

	# Activate process record/replay
	gdb_test_no_output "record" "Turn on process record"

	# Registers contents before each forward single step.
	set count 0
	for {} {$count < 500} {incr count} {
	    gdb_test_multiple "x/i \$pc" "" {
		-re ".* ($hex) <.*>:\[ \t\]*(.*)\r\n$gdb_prompt $" {
		    set insn_addr $expect_out(1,string)

		    if [expr {$last_insn == $insn_addr}] {
			break
		    }

		    set insn_array($count) $expect_out(2,string)
		}
	    }

	    set pre_regs($count) [capture_command_output "info all-registers" ""]
	    gdb_test "si" "" ""
	}

	incr count -1
	# Registers contents after each backward single step.
	for {set i $count} {$i >= 0} {incr i -1} {
	    gdb_test "reverse-stepi" "" ""
	    set post_regs($i) [capture_command_output "info all-registers" ""]
	}

	# Compare the register contents.
	for {set i 0} {$i < $count} {incr i} {
	    if { ![gdb_assert { [string compare $pre_regs($i) $post_regs($i)] == 0 } \
		      "compare registers on insn $i:$insn_array($i)"] } {

		foreach pre_line [split $pre_regs($i) \n] post_line [split $post_regs($i) \n] {
		    if { [string compare $pre_line $post_line] } {
			verbose -log " -:$pre_line"
			verbose -log " +:$post_line"
		    }
		}
	    }
	}
	gdb_test "record stop"
    }
}

test "load"
test "move"
test "adv_simd_mod_imm"
test "adv_simd_scalar_index"
test "adv_simd_smlal"
test "adv_simd_vect_shift"
